package com.arron.ioAndNio;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.net.InetSocketAddress;
import java.nio.ByteBuffer;
import java.nio.channels.SocketChannel;

/**
 * @Classname TestNonBlockingNioClient
 * @Date 2020/8/28 1:28
 * @Created by 刘
 */
public class TestNonBlockingNioClient {

    //非阻塞的网络通信

    //客户端
    public static void main(String[] args) throws IOException {

        //创建套接字
        SocketChannel open = SocketChannel.open(new InetSocketAddress("127.0.0.1", 8080));

        //开启非阻塞模式
        open.configureBlocking(false);

        ByteBuffer buffer = ByteBuffer.allocate(1024);
        //分配直接内存
//        ByteBuffer buffer1 = ByteBuffer.allocateDirect(1024);
        String s = null;
        BufferedReader bufferedReader =null;
        while (true){

            bufferedReader= new BufferedReader(new InputStreamReader(System.in));
            s = bufferedReader.readLine();
            if (s.equals("stop")){
                break;
            }
            buffer.put(s.getBytes());
            buffer.flip();
            open.write(buffer);
            buffer.clear();
        }

        open.close();

    }



}
